/*
 * Utilities.java
 *
 * Created on December 2, 2007, 3:38 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package filecopier.gui;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.text.JTextComponent;
import javax.swing.text.DefaultEditorKit;
import javax.swing.JPopupMenu;
import javax.swing.Action;
import java.util.HashMap;

/**
 *
 * @author david
 */
public class Utilities {

    /** Creates a new instance of Utilities */
    public Utilities() {
    }

    public synchronized static JPopupMenu createEditPopupMenu(JTextComponent textComponent) {
        HashMap actions = createActionTable(textComponent);
        JPopupMenu menu = new JPopupMenu();

        menu.add(getActionByName(actions, DefaultEditorKit.cutAction));
        menu.add(getActionByName(actions, DefaultEditorKit.copyAction));
        menu.add(getActionByName(actions, DefaultEditorKit.pasteAction));
        menu.add(getActionByName(actions, DefaultEditorKit.selectAllAction));

        return menu;
    }

    public synchronized static Action getActionByName(HashMap actions, String name) {
        return (Action) (actions.get(name));
    }

    public synchronized static HashMap createActionTable(JTextComponent textComponent) {
        HashMap actions = new HashMap();
        Action[] actionsArray = textComponent.getActions();
        for (int i = 0; i < actionsArray.length; i++) {
            Action a = actionsArray[i];
            actions.put(a.getValue(Action.NAME), a);
        }

        return actions;
    }
}
